﻿using PLC.Core.Entities;
using PLC.Interface.Variables;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using YWM;
using YWM.Application;
using YWM.Application.Linq.Extensions;
using YWM.Common.Extensions;
using YWM.Dto;
using YWM.Repositories;

namespace PLC.Application.Variables.Services
{
    internal class VariableService : AsyncCrudService<Variable, VariableResultDto, long, VariableResultRequestDto, VariableDto>, IVariableService
    {
        private readonly IRepository<Group> _groupRepository = YApp.Resolve<IRepository<Group>>();

        private readonly IRepository<StationVariable> _stationVariableRepository = YApp.Resolve<IRepository<StationVariable>>();

        public async Task<PagedResultDto<VariableResultDto>> GetCustomAll(VariableResultRequestDto input)
        {
            IQueryable<VariableResultDto> queryable = (from v in Repository.GetAll().OrderBy(v => v.StartIndex)
                                                       join g in _groupRepository.GetAll()
                                                       on v.GroupId equals g.Id into temp
                                                       from g in temp.DefaultIfEmpty()
                                                       select new VariableResultDto
                                                       {
                                                           Id = v.Id,
                                                           DisplayName = v.DisplayName,
                                                           GroupId = v.GroupId,
                                                           GroupName = g.DisplayName,
                                                           StartIndex = v.StartIndex,
                                                           OffsetOrLength = v.OffsetOrLength,
                                                           DataType = v.DataType,
                                                           Scale = v.Scale,
                                                           Offset = v.Offset,
                                                           PosAlarm = v.PosAlarm,
                                                           NegAlarm = v.NegAlarm,
                                                           Description = v.Description
                                                       }).WhereIf(!input.GroupName.IsNullOrWhiteSpace(), v => v.GroupName.Contains(input.GroupName));
            return await queryable.ToPagedResultAsync(input);
        }

        public override async Task Delete(EntityDto<long> input)
        {
            long id = input.Id;
            StationVariable stationVariable = await _stationVariableRepository.FirstOrDefaultAsync(sv => sv.StationAnomalyId == id || sv.TempId == id || sv.HumidityId == id || sv.TempHighId == id || sv.TempLowId == id || sv.HumidityHighId == id || sv.HumidityLowId == id || sv.TempHighLimitId == id || sv.TempLowLimitId == id || sv.HumidityHighLimitId == id || sv.HumidityLowLimitId == id || sv.TempAlarmEnableId == id || sv.HumidityAlarmEnableId == id);
            if (stationVariable != null)
            {
                throw Oh("该变量已被站点使用，不能删除");
            }
            await base.Delete(input);
        }
    }
}
